'''
Created on Oct 15, 2012

@author: daoxuandung
'''

'''
Mainly to manage memcache keys. All memcache keys is internally here
To get and set value will be done via method of this module
'''
from google.appengine.api import memcache
import json, logging

def key_for_notes_by_board(board_key):
    key = {}
    key['board_key'] = board_key.urlsafe()
    key['type'] = 'notes_by_board'
    return json.dumps(key)

def get_notes_by_board(board_key):
    # Construct memcache key
    key = key_for_notes_by_board(board_key)
    client = memcache.Client()
    return client.get(key)


def set_notes_by_board(board_key, data=None):
    key = key_for_notes_by_board(board_key)
    client = memcache.Client()
    client.set(key, data)
    
def invalidate_notes_by_board(board_key):
    logging.info(board_key)
    key = key_for_notes_by_board(board_key)
    client = memcache.Client()
    client.delete(key)
    

# For top votes articles
def key_for_top_vote_articles(day=None, month=None, year=None, start_cursor=None):
    key = {}
    key['day'] = day
    key['month'] = month
    key['year'] = year
    if start_cursor is not None:
        key['start_cursor'] = start_cursor.urlsafe()
    key['type'] = 'top_vote_articles'
    return json.dumps(key)

def get_top_vote_articles(day=None, month=None, year=None, start_cursor=None):
    logging.info("get top vote")
    key = key_for_top_vote_articles(day, month, year, start_cursor)
    client = memcache.Client()
    return client.get(key)

def set_top_vote_articles(day=None, month=None, year=None, start_cursor=None, data=None):
    # Cache for 3 seconds
    logging.info("set top vote")
    key = key_for_top_vote_articles(day, month, year, start_cursor)
    client = memcache.Client()
    return client.set(key, data, 3)
